﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Mock1G3.BaseEntities;

namespace Mock1G3.DataAccess
{
    public class ValidateAccess
    {
        public static bool CheckCardNo(Card card)
        {
            DataTable dt = DataAccess.ExeStoredWithParamToDataTable("CheckCardNo", "@cardNo", card.CardNo);
            if (dt.Rows.Count == 1)
                return true;
            else
                return false;
        }

        public static DataTable Table(Card card)
        {
            return DataAccess.ExeStoredWithParamToDataTable("CheckCard", "@cardNo", card.CardNo, "@pin", card.PIN);
        }

        public static bool CheckCard(Card card)
        {
            DataTable dt = DataAccess.ExeStoredWithParamToDataTable("CheckCard", "@cardNo", card.CardNo, "@pin", card.PIN);
            if (dt.Rows.Count == 1)
                return true;
            else
                return false;
        }

        public static int CheckAttempt(Card card)
        {
            return DataAccess.ExeStoredToInt("CheckAttempt", "@cardNo", card.CardNo);
        }

        public static bool CheckStatus(Card card)
        { 
            string status = DataAccess.ExeStoredToString("CheckStatus", "@cardNo", card.CardNo);
            if (status == "normal")
                return true;
            else
                return false;
        }

        public static bool CheckExpiredDate(Card card)
        {
            DateTime ngayHienTai = new DateTime();
            ngayHienTai = DateTime.Now.Date;
            DateTime expiredDate = DataAccess.ExeStoredToDate("CheckExpiredDate", "@cardNo", card.CardNo);
            if (ngayHienTai <= expiredDate)
                return true;
            else
                return false;
        }

        public static bool UpdateAttempt(Card card)
        {
            try
            {
                DataAccess.ExeStoredWithParamNon("UpdateAttempt", "@cardNo", card.CardNo, "@attempt", card.Attempt);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        public static bool UpdatePin(Card card)
        {
            try
            {
                DataAccess.ExeStoredWithParamNon("UpdatePIN", "@cardNo", card.CardNo, "@pin", card.PIN);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static bool WriteLog(Log log)
        {
            try
            {
                DataAccess.ExeStoredWithParamNon("LogInsertChangePin", "@ATMID", log.ATMID, "@CardNo", log.CardNo, "@LogDate", log.LogDate);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}